package com.devops.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devops.admin.model.Pagination;
import com.devops.admin.model.TableResult;
import com.devops.admin.model.ToolCategories;
import com.devops.admin.model.Tools;
import com.devops.admin.service.ToolCategoriesService;
import com.devops.admin.mapper.ToolCategoriesMapper;
import com.devops.admin.service.ToolsService;
import com.devops.admin.util.Assert;
import com.devops.admin.vo.DOSPageVo;
import com.devops.admin.vo.ToolCategoriesVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Dell
* @description 针对表【dos_tool_categories】的数据库操作Service实现
* @createDate 2024-03-08 11:39:59
*/
@Service
public class ToolCategoriesServiceImpl extends ServiceImpl<ToolCategoriesMapper, ToolCategories>
    implements ToolCategoriesService{
    @Resource
    private ToolsService toolsService;

    @Override
    public Integer addToolCategory(ToolCategories toolCategory) {
        save(toolCategory);
        return toolCategory.getId();
    }

    @Override
    public void updateToolCategory(ToolCategories toolCategory) {
        updateById(toolCategory);
    }

    @Override
    public void deleteToolCategory(Integer id) {
        Assert.isTrueMvc(toolsService.lambdaQuery().eq(Tools::getToolCategoryId, id).list().isEmpty(),"该分类下有工具，无法删除");
        removeById(id);
    }

    @Override
    public TableResult<ToolCategoriesVo> queryToolCategories(DOSPageVo pageVo) {
        Map<Integer, List<Tools>> toolsMap = toolsService.lambdaQuery()
                .orderByAsc(Tools::getSortOrder).list()
                .stream().collect(Collectors.groupingBy(Tools::getToolCategoryId));

        Page<ToolCategories> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        List<ToolCategoriesVo> toolCategories =
                lambdaQuery().orderByAsc(ToolCategories::getSortOrder).page(page).getRecords()
                        .stream().map(category -> {
                            ToolCategoriesVo categoriesVo = BeanUtil.toBean(category, ToolCategoriesVo.class);
                            categoriesVo.setToolsCount(toolsMap.get(category.getId()) == null? 0 : toolsMap.get(category.getId()).size());
                            return categoriesVo;
                        }).collect(Collectors.toList());

        Pagination p = Pagination.builder().pageNum(page.getCurrent()).pageSize(page.getSize()).total(page.getTotal()).build();
        return new TableResult<>(toolCategories, p);
    }

    @Override
    public List<ToolCategoriesVo> queryAllToolsMap() {
        Map<Integer, List<Tools>> toolsMap = toolsService.lambdaQuery()
                .orderByAsc(Tools::getSortOrder).list()
                .stream().collect(Collectors.groupingBy(Tools::getToolCategoryId));

        return lambdaQuery()
                .orderByAsc(ToolCategories::getSortOrder).list()
                .stream().map(category -> {
                    List<Tools> subToolList = toolsMap.get(category.getId());
                    ToolCategoriesVo categoriesVo = BeanUtil.toBean(category, ToolCategoriesVo.class);
                    categoriesVo.setList(subToolList);
                    return categoriesVo;
                }).collect(Collectors.toList());
    }
}




